/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_adc.c
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the adc module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-26  |V2.0     |Wang Junxiong|New creat
 *****************************************************************************/

/******************************************************************************
 **                     QAC Warnings
 *****************************************************************************/

/* PRQA S 0436 EOF #8-bit MCU Features: Direct Write Registers. */
/* PRQA S 1291, 1820, 1821, 1822, 1823, 1851, 1853, 3715, 3757, 3760 EOF #Registers are essentially unsigned numbers. */
/* PRQA S 2754 EOF #Level 7 problem due to unsupported assembly instructions. */
/* PRQA S 3768, 3771, 4130, 4393, 4434, 4443, 4446, 4461, 4532 EOF #Registers are essentially unsigned numbers. */
/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx_adc.h"
/******************************************************************************
 *                      Macro Definitions
 *****************************************************************************/

/******************************************************************************
 *                      Constants
 *****************************************************************************/

/******************************************************************************
 *                      Type definitions
 *****************************************************************************/

/******************************************************************************
 *                      Variables
 *****************************************************************************/

/******************************************************************************
 *                      Function definitions
 *****************************************************************************/
/**
 * @brief ADC Initialization
 *
 * @param Channel ADC channel
 * @param Clk Clock Selection
 * @param Vref Reference Voltage
 * @param Ali Data Alignment Mode
 */
void Adc_Init(Adc_ChannelType Channel, Adc_ClkType Clk, Adc_VrefType Vref, Adc_AlignmentType Ali)
{
    ADCCTL0   = (uint8_t)Ali | 1u;
    ADCCTL1   = (uint8_t)Clk | (uint8_t)Vref;
    ADCCTL2   = (uint8_t)Channel & 0x3fu;
    ADCINTCTL = 0u;
    if (Channel == ADC_TS_OUT)
    {
        TEMPSNR = 1u;
    }
    else
    {
        TEMPSNR = 0u;
    }
}

/**
 * @brief ADC Deinitialization
 *
 */
void Adc_DeInit(void)
{
    ADCCTL0   = 0x00u;
    ADCCTL1   = 0x00u;
    ADCCTL2   = 0x00u;
    ADCINTCTL = 0x00u;
    TEMPSNR   = 0x00u;
    T2CCR0H   = 0x00u;
    T2CCR0L   = 0x00u;
    T2CCR1H   = 0x00u;
    T2CCR1L   = 0x00u;
}

/**
 * @brief Enable or disable ADC
 *
 * @param State 0: disable, 1: enable
 */
void Adc_Enable(uint8_t State)
{
    ADEN = (uint8_t)State;
}

/**
 * @brief ADC Enable Conversion
 *
 */
void Adc_EnableConversion(void)
{
    START = 1u;
}

/**
 * @brief ADC single sample
 *
 * @return uint16_t ADC Sample Value
 */
uint16_t Adc_SingleSample(void)
{
    uint16_t adc_temp = 0u, timeout = 1280u;
    START = 1u;
    while ((START == 1u) && (timeout > 0u))
    {
        timeout -= 1u;
    }
    adc_temp = ADCDATA0H;
    adc_temp <<= 8u;
    adc_temp += ADCDATA0L;
    if (ADLR == 1u)
    {
        /* nothing */
    }
    else
    {
        adc_temp >>= 4u;
    }

    return adc_temp;
}

/**
 * @brief Initialize T2 trigger ADC
 *
 * @param Period T2CCR0H, T2CCR0L
 */
void Adc_T2ccr0Enable(uint16_t Period)
{
    T2CCR0H  = (uint8_t)(Period >> 8u);
    T2CCR0L  = (uint8_t)Period;
    T2CCR0ON = 1u;
}

/**
 * @brief Initialize T2 trigger ADC
 *
 * @param Period T2CCR1H, T2CCR1L
 */
void Adc_T2ccr1Enable(uint16_t Period)
{
    T2CCR1H  = (uint8_t)(Period >> 8u);
    T2CCR1L  = (uint8_t)Period;
    T2CCR1ON = 1u;
}

/**
 * @brief ADC get conversion value
 *
 * @return uint16_t ADC Sample Value
 */
uint16_t Adc_GetConversionValue(void)
{
    uint16_t adc_temp = 0u;

    adc_temp = ADCDATA0H;
    adc_temp <<= 8u;
    adc_temp += ADCDATA0L;
    if (ADLR == 1u)
    {
        /* nothing */
    }
    else
    {
        adc_temp >>= 4u;
    }

    return adc_temp;
}
/* EOF */
